<?php
require_once 'template.domain.php';
require_once 'lib/PhpTextSpinner.inc.php';
require_once 'lib/class.autokeyword.php';
require_once 'amazon.domain.php';
class WordpressXMLRPC {

    public $wp_username;
    public $wp_password;
    public $wp_url;
    public $wp_template;
    public $wp_category;

    public function post(Item $items,$customFields = array()) {
        /*** Custom_Fields ***/
        $newCustomFields = array();
        for ($i = 0; $i <= count($customFields); $i++) {
            $newCustomFields[$i]['key'] = $customFields[$i]['key'];
            $newCustomFields[$i]['value'] = WordpressXMLRPC::convertTAG($items,$customFields[$i]['value']);
        }

        /*** Template ***/
        $templateObj = new Template();
        $template = $templateObj->getByID($this->wp_template);
        /*** Convert Tag ***/
        $content = WordpressXMLRPC::convertTAG($items,$template[0]['template_value']);
        $content = WordpressXMLRPC::convertTAG($items,$content);

        /*** XMLRCP Object ***/
        $wp_url = $this->wp_url . '/xmlrpc.php';
        $wp_obj = new IXR_Client($wp_url);

        /*** Add Category Wordpres ***/
        $category = array(
                'name' => $this->wp_category
        );
        $params = array(0,$this->wp_username,$this->wp_password,$category);

        if (!$wp_obj->query('wp.newCategory', $params)) {
            return '***'.$wp_obj->getErrorCode().' : '.$wp_obj->getErrorMessage().'***';
        }

        $phpTextSpinner=new PhpTextSpinner();

        //$spinContent=$phpTextSpinner->spinContent($content);
        $content=$phpTextSpinner->runTextSpinner($content);

        /*** Generate Tags ***/
        /*
        $params['content'] = $items->Content;

        $params['min_3words_length'] = 3;  //minimum length of words for 3 word phrases
        $params['min_3words_phrase_length'] = 10; //minimum length of 3 word phrases
        $params['min_3words_phrase_occur'] = 2; //minimum occur of 3 words phrase

        $tags = new autokeyword($params, "iso-8859-1");
         *
         */
    

        $data = array(
                'title'=> (string) $items->Title,
                'description'=> $content,
                'mt_allow_comments'=>0,  // 1 to allow comments
                'mt_allow_pings'=>0,  // 1 to allow trackbacks
                'post_type'=>'post',
                'mt_keywords'=>$tags,
                'categories'=>$category,
                'custom_fields' => $newCustomFields
        );

        $params = array(0,$this->wp_username,$this->wp_password,$data,true);

        if (!$wp_obj->query('metaWeblog.newPost', $params)) {
            return '***'.$wp_obj->getErrorCode().' : '.$wp_obj->getErrorMessage().'***';
            flush();
        }else {

            $amazonObj = new Amazon();
            $amazonObj->updateByASIN($items->ASIN);
            return '***Posted ' . $items->ASIN .' Successfully***';
            flush();
        }


    }

    public function convertTAG(Item $items,$value) {

        $ch = curl_init();
        curl_setopt($ch,CURLOPT_URL,(string)$items->CustomerReviews);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,5);
        $revcontent = curl_exec($ch);
        curl_close($ch);

        $revcontent = ereg_replace('[[:space:]]+', ' ', trim($revcontent));

        if (preg_match('~<body[^>]*>(.*?)</body>~si', $revcontent, $body)) {
            $reviewsnoiframe = str_replace('class="crVotingButtons">', "", $body[1]);
        } else {
            $reviewsnoiframe = "";
        }

        if($reviewsnoiframe != "") {
            $reviewsnoiframe = str_replace('class="crIFrame"', ' style="margin:0px;"', $reviewsnoiframe);
            $reviewsnoiframe = str_replace('class="crIFrameLogo"', ' style="margin-bottom:20px;display:none;"', $reviewsnoiframe);
            $reviewsnoiframe = str_replace('class="crIFrameHeaderLeftColumn"', ' style="float:left;margin-right:20px;display:none;"', $reviewsnoiframe);
            $reviewsnoiframe = str_replace('class="crIframeHeaderTitle"', ' style="font-size:24px;letter-spacing:-1px;"', $reviewsnoiframe);
            $reviewsnoiframe = str_replace('class="crIFrameAverageReviewText"', ' style="font-weight:bold;"', $reviewsnoiframe);
            $reviewsnoiframe = str_replace('class="crIFrameNumCustReviews"', ' style="white-space:nowrap;"', $reviewsnoiframe);
            $reviewsnoiframe = str_replace('class="crIFrameHeaderHistogram"', ' style="float:left;display:none;"', $reviewsnoiframe);
            $reviewsnoiframe = str_replace('class="tiny"', ' style=font-family:verdana,arial,helvetica,sans-serif;font-size:x-small;"', $reviewsnoiframe);
            $reviewsnoiframe = str_replace('class="crIframeReviewList"', ' style="clear:left;width:100%;"', $reviewsnoiframe);
            $reviewsnoiframe = str_replace('<a ', ' <a rel="nofollow" ', $reviewsnoiframe);
            $reviewsnoiframe = str_replace('<div style="padding-top: 10px; clear: both; width: 100%;"', ' <div style="padding-top: 10px; clear: both; width: 100%;display:none;"', $reviewsnoiframe);
            $reviewsnoiframe = str_replace('class="crIFrameCreateReview"', ' style="display:none;"', $reviewsnoiframe);
            $reviewsnoiframe = str_replace('class="tiny verifyWhatsThis"', ' style="display:none;"', $reviewsnoiframe);
            $reviewsnoiframe = str_replace('href="/gp/help/customer/display.html/', ' href="http://www.amazon.'.$site.'/gp/help/customer/display.html/', $reviewsnoiframe);
        }


        $data = str_replace('{Title}', (string)$items->Title, $value);
        $data = str_replace('{Content}', (string)$items->Content, $data);
        $data = str_replace('{AppContent}',(string)$items->AppContent,$data);
        $data = str_replace('{ASIN}',(string)$items->ASIN,$data);
        $data = str_replace('{DetailPageURL}',(string)$items->DetailPageURL,$data);
        $data = str_replace('{ListPrice}',(string)$items->ListPrice,$data);
        $data = str_replace('{Price}',(string)$items->Price,$data);
        $data = str_replace('{SmallImage}',(string) $items->SmallImage, $data);
        $data = str_replace('{MediumImage}',(string) $items->MediumImage, $data);
        $data = str_replace('{CustomerReviews}',(string) $reviewsnoiframe, $data);

        return $data;
    }
}
?>
